package com.bbf.springmvcdemo.feature;

import com.bbf.springmvcdemo.util.BaseUtil;
import com.bbf.springmvcdemo.util.WebPropertyHolder;
import org.apache.commons.lang3.StringUtils;

/**
 * 全局配置类
 * @author BBF
 */
public class CustomProperties extends BaseBean {
  private static final long serialVersionUID = -3823804773124959970L;

  private CustomProperties() {
  }

  private enum CustomPropertiesFactory {
    /**
     * 单例枚举
     */
    INSTANCE;
    private CustomProperties instance;

    CustomPropertiesFactory() {
      instance = new CustomProperties();
    }

    public CustomProperties getInstance() {
      return instance;
    }
  }

  /**
   * 获取单例对象
   * @return CustomProperties对象
   */
  public static CustomProperties getInstance() {
    return CustomPropertiesFactory.INSTANCE.getInstance();
  }

  /**
   * 获取配置
   * @param key 属性配置key
   * @return 属性值
   */
  public static String getConfig(String key) {
    return WebPropertyHolder.getContextProperty(key);
  }

  public static String getAuthor() {
    return getConfig("author");
  }

  public static String getShowMessage() {
    //这里需要明确是否进行转码
    return convertToUtf8(getConfig("showMessage"));
  }

  public static boolean isBlank(String str){
    return StringUtils.isBlank(str);
  }

  public static boolean isNoneBlank(String str){
    return StringUtils.isNoneBlank(str);
  }

  /**
   * 将properties文件中出现的中文转换为英文
   * @param msg 中文乱码
   * @return 正确的中文
   */
  private static String convertToUtf8(String msg) {
    return new String(msg.getBytes(BaseUtil.CHARSET_ISO859), BaseUtil.CHARSET_UTF8);
  }
}
